package practice1_100;

public class Practice114 {
    TreeNode temp,re;
    public void flatten(TreeNode root) {
        TreeNode head=root;
        dp(root);
        System.out.println(head);
    }
    public void dp(TreeNode root){
        if(root==null) return;
        dp(root.left);
        dp(root.right);

        if(root.left!=null) {
            temp = root.right;
            root.right= root.left;
            root.left=null;
            re=root.right;
            while (re.right!=null){
                re=re.right;//
            }
            re.right=temp;
        }
    }

    public static void main(String[] args) {
        TreeNode root=new TreeNode(1);
        root.left=new TreeNode(2);
        root.right=new TreeNode(3);
        root.left.left=new TreeNode(4);
        root.left.right=new TreeNode(5);
        root.right.left=new TreeNode(6);
        root.right.right=new TreeNode(7);

        new Practice114().flatten(root);
    }
}
